<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class Angerbird extends Base
{
    protected $abConfigTable = 'angerbird_config';
    protected $abEventTable = 'angerbird_event';
    protected $abStatisticsTable = 'angerbird_statistics';
    protected $abShopTable = 'angerbird_shop';
    protected $giftTable = 'gift_library';

    //愤怒的小鸟游戏配置
    protected $configKey = 'ANGERBIRD:CONFIG';//string ANGERBIRD:CONFIG => {string}

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    //获取愤怒的小鸟游戏配置
    public function getConfig($key='')
    {
        $cacheKey = $this->configKey;
        $data = Redis::get($cacheKey);
        if (!empty($data)) {
            $data = json_decode($data, true);
        }
        if(empty($data)) {
            $data = DB::table($this->abConfigTable)->select(['id','key','value'])->get();
            $data = objToArray($data);
            if(!empty($data)) {
                Redis::set($cacheKey, json_encode($data, JSON_UNESCAPED_UNICODE));
                Redis::expire($cacheKey, 86400);
            }
        }
        if(!empty($key)) {
            $data = empty($data) ? [] : array_column($data, 'value', 'key');
            return empty($data[$key]) ? '' : $data[$key];
        }

        return $data;
    }

    //更新配置
    public function updateConfig($key, $value)
    {
        DB::table($this->abConfigTable)
            ->where('key', $key)
            ->update(['value'=>$value]);

        //删除缓存
        $cacheKey = $this->configKey;
        Redis::del($cacheKey);

        return 1;
    }

    //愤怒的小鸟获取商店指定礼物
    public function getAngerbirdShopByGiftId($giftId)
    {
        $data = DB::table($this->abShopTable)->where('gift_id', $giftId)->first();
        $data = objToArray($data);

        return $data;
    }

    //愤怒的小鸟获取商店指定记录
    public function getAngerbirdShopById($id)
    {
        $data = DB::table($this->abShopTable)->where('id', $id)->first();
        $data = objToArray($data);

        return $data;
    }

    //愤怒的小鸟新增商店
    public function angerbirdAddShop($giftId, $energyCoin, $ifShow)
    {
        $data = [];
        $data['gift_id'] = $giftId;
        $data['energy_coin'] = $energyCoin;
        $data['if_show'] = $ifShow;

        $id = DB::table($this->abShopTable)->insertGetId($data);

        return $id;
    }

    //愤怒的小鸟修改商店
    public function angerbirdUpdateShop($id, $params)
    {
        $upData = [];
        if(!empty($params['energy_coin'])) {
            $upData['energy_coin'] = $params['energy_coin'];
        }
        if(!empty($params['if_show'])) {
            $upData['if_show'] = $params['if_show'];
        }
        if(empty($upData)) {
            return 0;
        }

        $res = DB::table($this->abShopTable)->where('id', $id)->update($upData);

        return $res;
    }

    //愤怒的小鸟兑换商店列表
    public function getAngerbirdShopList()
    {
        $data = DB::table($this->abShopTable.' as a')
            ->leftJoin($this->giftTable.' as b', 'a.gift_id', '=', 'b.gift_id')
            ->where('a.if_show', 1)
            ->select(['a.id','a.gift_id','a.energy_coin','a.if_show','b.name as gift_name','b.image as gift_image','b.price as gift_price'])
            ->orderBy('a.energy_coin', 'desc')
            ->get();
        $data = objToArray($data);
        foreach($data as $key=>$val) {
            $val['gift_image'] = build_image($val['gift_image']);
            $data[$key] = $val;
        }

        return $data;
    }

    //记录愤怒的小鸟用户押注记录
    public function createLog($data)
    {
        $res = DB::table($this->abEventTable)->insertGetId($data);
        return $res;
    }

    //更新愤怒的小鸟用户押注记录
    public function updateLog($gameNum, $userId, $params)
    {
        $upData = [];
        if(!empty($params['basePrice'])) {
            $upData['basePrice'] = $params['basePrice'];
        }
        if(!empty($params['rewardPrice'])) {
            $upData['rewardPrice'] = $params['rewardPrice'];
        }
        if(!empty($params['r_height'])) {
            $upData['r_height'] = $params['r_height'];
        }
        if(!empty($params['status'])) {
            $upData['status'] = $params['status'];
        }
        if(!empty($params['is_delete'])) {
            $upData['is_delete'] = $params['is_delete'];
        }

        if(empty($upData)) {
            return 0;
        }

        $res = DB::table($this->abEventTable)
            ->where('game_num', $gameNum)
            ->where('user_id', $userId)
            ->where('is_delete', 0)
            ->where('status', 0)
            ->update($upData);
        return $res;
    }

    //愤怒的小鸟获取指定游戏押注记录
    public function getAngerbirdLogByGameNum($gameNum, $height)
    {
        $data = DB::table($this->abEventTable)
            ->where('game_num', $gameNum)
            ->where('height', '<=', $height)
            ->where('status', 0)
            ->where('is_delete', 0)
            ->get();
        $data = objToArray($data);

        return $data;
    }

    //愤怒的小鸟获取指定游戏中奖押注记录
    public function getAngerbirdLogWin($gameNum, $limit=5)
    {
        $data = DB::table($this->abEventTable)
            ->where('game_num', $gameNum)
            ->where('status', 1)
            ->where('is_delete', 0)
            ->orderBy('rewardPrice', 'desc')
            ->limit($limit)
            ->get();
        $data = objToArray($data);

        return $data;
    }

    //记录统计数据
    public function createStatLog($data)
    {
        $res = DB::table($this->abStatisticsTable)->insertGetId($data);
        return $res;
    }

    //游戏数据汇总
    public function getGameSumByGameNum($gameNum)
    {
        $query = DB::table($this->abEventTable)
            ->selectRaw('sum(stakeCount) as stakeCount, count(distinct user_id) as userNum, sum(basePrice) as basePrice, sum(rewardPrice) as rewardPrice')
            ->where('game_num', $gameNum)
            ->where('is_delete', 0);
        $row = $query->first();
        $row = self::objToArray($row);

        return $row;
    }

    //游戏数据汇总
    public function getGameSumByGameNumWin($gameNum)
    {
        $query = DB::table($this->abEventTable)
            ->selectRaw('count(1) as stakeCount, count(distinct user_id) as userNum, sum(basePrice) as basePrice, sum(rewardPrice) as rewardPrice')
            ->where('game_num', $gameNum)
            ->where('is_delete', 0)
            ->where('status', 1);
        $row = $query->first();
        $row = self::objToArray($row);

        return $row;
    }

    //游戏数据汇总
    public function getGameStatList($limit=50)
    {
        $list = DB::table($this->abStatisticsTable)->limit($limit)->orderBy('id', 'desc')->get();
        $list = self::objToArray($list);

        return $list;
    }

    /**
     * 用户飞行记录列表
     */
    public function userLogList($page, $limit, $where){
        $query = DB::table($this->abEventTable)->where('is_delete', 0);
        if(!empty($where['user_id'])) {
            $query = $query->where('user_id', $where['user_id']);
        }
        $query = $query->orderBy('game_num', 'desc')
            ->offset($page * $limit)->limit($limit);
        $list = $query->get();
        $list = objToArray($list);

        return $list;
    }

}
